package classconverstation.cscl.ist.psu.edu.client.classdisplay;

import classconverstation.cscl.ist.psu.edu.client.messengers.ClassCommonsMessage;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsService;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsServiceAsync;
import classconverstation.cscl.ist.psu.edu.client.util.ClassConversationUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

public class StackMessage extends Composite {
	
	private ClassCommonsMessage msg;
	private FlowPanel mainPanel;
	private StackMessage thisStack = this;
	
	Image video = new Image("../images/youtube.png");
	Image image = new Image("../images/image.png");
	
	public StackMessage(ClassCommonsMessage msg){
		this.msg = msg;

		this.mainPanel = new FlowPanel();
		initWidget(mainPanel);
		//mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		Label nameLabel = new Label(msg.getPoster().getFName()+": ");
		nameLabel.addStyleDependentName("nameLabel");
		Label qLabel = new Label(msg.getContent());
		qLabel.addStyleDependentName("qLabel");
		FlexTable fTable = new FlexTable();
		fTable.setWidget(0, 0, nameLabel);
		fTable.setWidget(0, 1, qLabel);
		mainPanel.add(fTable);
		
		addMultiMedia();
		addOptions();
		

		mainPanel.addStyleName("stack");
	}
	
	public void addMultiMedia(){
		FlexTable multimediaTable = new FlexTable();
		int column = 0;

		if (!ClassConversationUtil.IsNullOrEmpty(msg.getImageURL())){
			multimediaTable.setWidget(0, column, image);
			column++;
			image.addStyleName("pointer");
			image.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					final PopupPanel imagePop = new PopupPanel();
					Image fullImage = new Image(msg.getImageURL());
					fullImage.setAltText("Loading image ...");
			        fullImage.addLoadHandler(new LoadHandler() {
			            public void onLoad(LoadEvent event) {
			            	imagePop.center();
							
						}
			        });
			        imagePop.setWidget(fullImage);
					imagePop.setAutoHideEnabled(true);

			        imagePop.center();
					
				}
				
			});
			
		}
		if (!ClassConversationUtil.IsNullOrEmpty(msg.getVideoURL())){
			multimediaTable.setWidget(0, column, video);
			video.addStyleName("pointer");
			
			video.addClickHandler(new ClickHandler(){

				public void onClick(ClickEvent event) {
					String vid = msg.getVideoID();
					System.out.println("Video ID "+vid);
					Frame frame = new Frame("http://www.youtube.com/embed/"+vid);
					frame.setPixelSize(420, 315);
					
					PopupPanel videoPop = new PopupPanel();
					videoPop.setWidget(frame);
					videoPop.setAutoHideEnabled(true);
					videoPop.center();					
				}
			});
		}
		mainPanel.add(multimediaTable);
	}
	
	public void addOptions(){
		FlowPanel  metaPanel = new FlowPanel();
		metaPanel.addStyleName("metaPanel");
		FlexTable metaTable = new FlexTable();
		DateTimeFormat fmt = DateTimeFormat.getFormat("hh:mm aaa, MM/dd, yyyy");
		Label timeLabel = new Label(fmt.format(msg.getPostDate()));
		metaTable.setWidget(0, 0, timeLabel);
		mainPanel.add(metaTable);

	}

}
